package com.cn.qgx428.orderingmeal.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.cn.qgx428.orderingmeal.R;
import com.cn.qgx428.orderingmeal.adapter.ShopAdapter;
import com.cn.qgx428.orderingmeal.myClass.MyKeyInfo;
import com.cn.qgx428.orderingmeal.myClass.Shop;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * A simple {@link Fragment} subclass.
 */
public class FoodFragment extends Fragment {

    private static final String TAG="FoodFragment";

    private View view = null;
    private RecyclerView shopRecyler;
    private ShopAdapter shopAdapter;
    private LinearLayoutManager linearLayoutManager;
    private MyKeyInfo keyInfo;

    private int dataAmount = 3; //返回的数据
    private int dataAlready = 3; //当前已加载的数据
    private int lastVisibleItem;


    public FoodFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_food, container, false);
        Bmob.initialize(getActivity(),"1b925fe2ccbb402f4034a3e28c31ad93");
        init();
        getShopData();
        return view;
    }

    private void getShopData() {
        BmobQuery<Shop> query = new BmobQuery<Shop>();
        query.setLimit(200);
        query.findObjects(new FindListener<Shop>() {
            @Override
            public void done(List<Shop> list, BmobException e) {
                if (e == null){
                    shopAdapter = new ShopAdapter(getActivity(),list,keyInfo);
                    shopRecyler.setAdapter(shopAdapter);
                } else {
                    Toast.makeText(getActivity(), "服务器出错", Toast.LENGTH_SHORT).show();
                    Log.d(TAG, "done: "+e.getErrorCode()+e.getMessage());
                }
            }
        });
    }

    private void init() {
        keyInfo =(MyKeyInfo) getActivity().getIntent().getSerializableExtra("keyInfo");

        shopRecyler = (RecyclerView) view.findViewById(R.id.food_shopRecyclerView);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
        shopRecyler.setLayoutManager(linearLayoutManager);
        //设置foodRecyler加载事件
        shopRecyler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem+1 == shopAdapter.getItemCount()){
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            BmobQuery<Shop> query = new BmobQuery<Shop>();
                            query.setLimit(dataAmount);
                            query.setSkip(dataAlready);
                            query.findObjects(new FindListener<Shop>() {
                                @Override
                                public void done(List<Shop> list, BmobException e) {
                                    if (e == null){
                                        shopAdapter.addMoreData(list);
                                        dataAlready+=dataAmount;
                                    } else {
                                        Toast.makeText(getActivity(), "服务器出错", Toast.LENGTH_SHORT).show();
                                        Log.d(TAG, "done: "+e.getErrorCode()+e.getMessage());
                                    }
                                }
                            });
                        }
                    },0);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
            }
        });
    }
}
